/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZGrant;
/*     */ import com.zimbra.client.ZGrant.GranteeType;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.Options;
/*     */ import com.zimbra.client.ZSearchParams;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.service.AuthProviderException;
/*     */ import com.zimbra.cs.service.UserServlet;
/*     */ import com.zimbra.cs.servlet.ZimbraServlet;
/*     */ import java.io.PrintStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import junit.framework.TestCase;
/*     */ import org.apache.commons.httpclient.Credentials;
/*     */ import org.apache.commons.httpclient.Header;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.HttpState;
/*     */ import org.apache.commons.httpclient.UsernamePasswordCredentials;
/*     */ import org.apache.commons.httpclient.auth.AuthScope;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestAccessKeyGrant
/*     */   extends TestCase
/*     */ {
/*     */   private static final String DUMMY_AUTH_PROVIDER = "DUMMY_AUTH_PROVIDER";
/*     */   private static final String OWNER_NAME = "user1";
/*     */   private static final String USER_GRANTEE_NAME = "user3";
/*     */   private static final String ACCESS_KEY = "b931d99fc5dc7e8061a97d90e05e3256";
/*     */   private static final String AUTH_K_ATTR = "K";
/*     */   private static final String AUTH_H_ATTR = "H";
/*     */   private static final String FOLDER_PATH = "Calendar";
/*     */   
/*     */   public static class DummyAuthProvider
/*     */     extends AuthProvider
/*     */   {
/*     */     public DummyAuthProvider()
/*     */     {
/*  87 */       super();
/*     */     }
/*     */     
/*     */ 
/*     */     protected AuthToken authToken(HttpServletRequest req, boolean isAdminReq)
/*     */       throws AuthProviderException, AuthTokenException
/*     */     {
/*  94 */       if (isAdminReq) {
/*  95 */         return null;
/*     */       }
/*  97 */       String accessKey = req.getParameter("k");
/*  98 */       String hostYid = req.getParameter("h");
/*     */       
/* 100 */       if ((accessKey != null) && (hostYid != null)) {
/* 101 */         return new TestAccessKeyGrant.DummyAuthToken(accessKey, hostYid);
/*     */       }
/* 103 */       throw AuthProviderException.NO_AUTH_DATA();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     protected AuthToken authToken(Element soapCtxt, Map engineCtxt)
/*     */       throws AuthProviderException, AuthTokenException
/*     */     {
/* 112 */       if (soapCtxt == null) {
/* 113 */         throw AuthProviderException.NO_AUTH_DATA();
/*     */       }
/*     */       try {
/* 116 */         Element eAuthToken = soapCtxt.getElement("authToken");
/* 117 */         String type = eAuthToken.getAttribute("type");
/* 118 */         if ((type == null) || (!type.equals("DUMMY_AUTH_PROVIDER"))) {
/* 119 */           throw AuthProviderException.NOT_SUPPORTED();
/*     */         }
/* 121 */         String ownerId = null;
/* 122 */         String accessKey = null;
/*     */         
/* 124 */         for (Element authAttr : eAuthToken.listElements("a")) {
/* 125 */           String name = authAttr.getAttribute("n");
/*     */           
/* 127 */           if ((name != null) && (name.equals("K"))) {
/* 128 */             accessKey = authAttr.getText();
/* 129 */           } else if ((name != null) && (name.equals("H"))) {
/* 130 */             ownerId = authAttr.getText();
/*     */           }
/*     */         }
/* 133 */         if ((accessKey != null) && (ownerId != null)) {
/* 134 */           return new TestAccessKeyGrant.DummyAuthToken(accessKey, ownerId);
/*     */         }
/*     */       } catch (ServiceException x) {
/* 137 */         throw AuthProviderException.NO_AUTH_DATA();
/*     */       }
/*     */       
/* 140 */       throw AuthProviderException.NO_AUTH_DATA();
/*     */     }
/*     */     
/*     */     protected boolean allowURLAccessKeyAuth(HttpServletRequest req, ZimbraServlet servlet)
/*     */     {
/* 145 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class DummyAuthToken extends AuthToken
/*     */   {
/*     */     private final String mAccessKey;
/*     */     private final String mOwnerId;
/*     */     
/*     */     DummyAuthToken(String accessKey, String ownerId)
/*     */     {
/* 156 */       this.mAccessKey = accessKey;
/* 157 */       this.mOwnerId = ownerId;
/*     */     }
/*     */     
/*     */     public void encode(HttpClient client, HttpMethod method, boolean isAdminReq, String cookieDomain)
/*     */       throws ServiceException
/*     */     {
/* 163 */       throw ServiceException.FAILURE("Not implemented", null);
/*     */     }
/*     */     
/*     */     public void encode(HttpState state, boolean isAdminReq, String cookieDomain)
/*     */       throws ServiceException
/*     */     {
/* 169 */       throw ServiceException.FAILURE("Not implemented", null);
/*     */     }
/*     */     
/*     */ 
/*     */     public void encode(HttpServletResponse resp, boolean isAdminReq, boolean secureCookie, boolean rememberMe)
/*     */       throws ServiceException
/*     */     {
/* 176 */       throw ServiceException.FAILURE("Not implemented", null);
/*     */     }
/*     */     
/*     */ 
/*     */     public void encodeAuthResp(Element parent, boolean isAdmin)
/*     */       throws ServiceException
/*     */     {}
/*     */     
/*     */     public String getAccountId()
/*     */     {
/* 186 */       return this.mOwnerId;
/*     */     }
/*     */     
/*     */     public String getAdminAccountId()
/*     */     {
/* 191 */       return null;
/*     */     }
/*     */     
/*     */     public String getCrumb() throws AuthTokenException
/*     */     {
/* 196 */       return null;
/*     */     }
/*     */     
/*     */     public String getDigest()
/*     */     {
/* 201 */       return null;
/*     */     }
/*     */     
/*     */     public String getAccessKey()
/*     */     {
/* 206 */       return this.mAccessKey;
/*     */     }
/*     */     
/*     */     public String getEncoded() throws AuthTokenException
/*     */     {
/* 211 */       return null;
/*     */     }
/*     */     
/*     */     public long getExpires()
/*     */     {
/* 216 */       return 0L;
/*     */     }
/*     */     
/*     */     public String getExternalUserEmail()
/*     */     {
/* 221 */       return null;
/*     */     }
/*     */     
/*     */     public boolean isAdmin()
/*     */     {
/* 226 */       return false;
/*     */     }
/*     */     
/*     */     public boolean isDomainAdmin()
/*     */     {
/* 231 */       return false;
/*     */     }
/*     */     
/*     */     public boolean isDelegatedAdmin()
/*     */     {
/* 236 */       return false;
/*     */     }
/*     */     
/*     */     public boolean isExpired()
/*     */     {
/* 241 */       return false;
/*     */     }
/*     */     
/*     */     public void deRegister()
/*     */       throws AuthTokenException
/*     */     {}
/*     */     
/*     */     public boolean isRegistered()
/*     */     {
/* 250 */       return true;
/*     */     }
/*     */     
/*     */     public boolean isZimbraUser()
/*     */     {
/* 255 */       return this.mAccessKey == null;
/*     */     }
/*     */     
/*     */     public String toString()
/*     */     {
/* 260 */       return null;
/*     */     }
/*     */     
/*     */     public ZAuthToken toZAuthToken() throws ServiceException
/*     */     {
/* 265 */       Map<String, String> attrs = new HashMap();
/* 266 */       attrs.put("K", this.mAccessKey);
/* 267 */       attrs.put("H", this.mOwnerId);
/* 268 */       return new ZAuthToken("DUMMY_AUTH_PROVIDER", null, attrs);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private Account getAccount(String acctName)
/*     */     throws ServiceException
/*     */   {
/* 278 */     Provisioning prov = Provisioning.getInstance();
/* 279 */     Account acct = prov.get(Key.AccountBy.name, acctName);
/* 280 */     assertNotNull(acct);
/* 281 */     return acct;
/*     */   }
/*     */   
/*     */   private String getAccountId(String acctName) throws ServiceException {
/* 285 */     return getAccount(acctName).getId();
/*     */   }
/*     */   
/*     */   private String getRestUrl(String acctName) throws ServiceException {
/* 289 */     return UserServlet.getRestUrl(getAccount(acctName));
/*     */   }
/*     */   
/*     */   private String getRestCalendarUrl(String acctName) throws ServiceException {
/* 293 */     return UserServlet.getRestUrl(getAccount(acctName)) + "/Calendar";
/*     */   }
/*     */   
/*     */   private ZMailbox getZMailboxByKey() throws Exception
/*     */   {
/* 298 */     Map<String, String> authAttrs = new HashMap();
/* 299 */     authAttrs.put("K", "b931d99fc5dc7e8061a97d90e05e3256");
/* 300 */     authAttrs.put("H", getAccountId("user1"));
/*     */     
/* 302 */     ZAuthToken zat = new ZAuthToken("DUMMY_AUTH_PROVIDER", null, authAttrs);
/* 303 */     ZMailbox.Options options = new ZMailbox.Options(zat, TestUtil.getSoapUrl());
/* 304 */     return ZMailbox.getMailbox(options);
/*     */   }
/*     */   
/*     */   private void dumpGrants(ZMailbox mbox, String folderId) throws Exception {
/* 308 */     ZFolder folder = mbox.getFolderRequestById(folderId);
/*     */     
/* 310 */     System.out.println("--------------------");
/* 311 */     System.out.println(mbox.getName() + ", folder=" + folderId);
/* 312 */     List<ZGrant> grants = folder.getGrants();
/* 313 */     for (ZGrant grant : grants) {
/* 314 */       System.out.println("    type: " + grant.getGranteeType().toString());
/* 315 */       System.out.println("    id: " + grant.getGranteeId());
/* 316 */       System.out.println("    name: " + grant.getGranteeName());
/* 317 */       System.out.println("    rights: " + grant.getPermissions());
/*     */       
/* 319 */       if (grant.getGranteeType() == ZGrant.GranteeType.key) {
/* 320 */         System.out.println("    accesskey: " + grant.getArgs());
/* 321 */       } else if (grant.getGranteeType() == ZGrant.GranteeType.guest) {
/* 322 */         System.out.println("    password: " + grant.getArgs());
/*     */       } else {
/* 324 */         assertNull(grant.getArgs());
/*     */       }
/* 326 */       System.out.println();
/*     */     }
/*     */   }
/*     */   
/*     */   public void disable_testKeyGrant() throws Exception {
/* 331 */     ZMailbox mbox = getZMailboxByKey();
/*     */     
/*     */ 
/* 334 */     StringBuffer query = new StringBuffer();
/* 335 */     query.append("(inid:");
/*     */     
/* 337 */     query.append("10");
/* 338 */     query.append(")");
/* 339 */     ZSearchParams sp = new ZSearchParams(query.toString());
/* 340 */     sp.setTypes("appointment");
/* 341 */     mbox.search(sp);
/*     */   }
/*     */   
/*     */ 
/*     */   public void disable_testGetFolderRequest()
/*     */     throws Exception
/*     */   {
/* 348 */     ZMailbox ownerMbox = TestUtil.getZMailbox("user1");
/* 349 */     ZMailbox granteeMbox = TestUtil.getZMailbox("user3");
/*     */     
/*     */ 
/* 352 */     String mointpointPath = "user1's Calendar";
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 371 */     dumpGrants(TestUtil.getZMailbox("user2"), "259");
/* 372 */     dumpGrants(TestUtil.getZMailbox("user4"), "258");
/*     */   }
/*     */   
/*     */   private void executeHttpMethod(HttpClient client, HttpMethod method) throws Exception
/*     */   {
/*     */     try {
/* 378 */       int respCode = HttpClientUtil.executeMethod(client, method);
/*     */       
/* 380 */       if (respCode != 200) {
/* 381 */         System.out.println("failed, respCode=" + respCode);
/*     */       }
/*     */       else {
/* 384 */         boolean chunked = false;
/* 385 */         boolean textContent = false;
/*     */         
/* 387 */         System.out.println("Headers:");
/* 388 */         System.out.println("--------");
/* 389 */         for (Header header : method.getRequestHeaders()) {
/* 390 */           System.out.print("    " + header.toString());
/*     */         }
/* 392 */         System.out.println();
/*     */         
/* 394 */         System.out.println("Body:");
/* 395 */         System.out.println("-----");
/* 396 */         String respBody = method.getResponseBodyAsString();
/* 397 */         System.out.println(respBody);
/*     */       }
/*     */     }
/*     */     finally {
/* 401 */       method.releaseConnection();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void disable_testCalendarGet_Yahoo_accesskey()
/*     */     throws Exception
/*     */   {
/* 438 */     HttpClient client = new HttpClient();
/*     */     
/* 440 */     String accessKey = "3c4877ed3948511cee39379debbf968d-bogus";
/* 441 */     String url = getRestCalendarUrl("user1");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 447 */     url = url + "?k=" + accessKey + "&h=" + getAccountId("user1");
/*     */     
/* 449 */     System.out.println("REST URL: " + url);
/* 450 */     HttpMethod method = new GetMethod(url);
/*     */     
/* 452 */     executeHttpMethod(client, method);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testCalendarGet_guest()
/*     */     throws Exception
/*     */   {
/* 461 */     HttpState initialState = new HttpState();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 470 */     String guestName = "g1@guest.com";
/* 471 */     String guestPassword = "zzz";
/* 472 */     Credentials loginCredentials = new UsernamePasswordCredentials(guestName, guestPassword);
/* 473 */     initialState.setCredentials(AuthScope.ANY, loginCredentials);
/*     */     
/* 475 */     HttpClient client = new HttpClient();
/* 476 */     client.setState(initialState);
/*     */     
/* 478 */     String url = getRestCalendarUrl("user1");
/* 479 */     System.out.println("REST URL: " + url);
/* 480 */     HttpMethod method = new GetMethod(url);
/*     */     
/* 482 */     executeHttpMethod(client, method);
/*     */   }
/*     */   
/*     */   public static void main(String[] args)
/*     */     throws Exception
/*     */   {
/* 488 */     CliUtil.toolSetup();
/* 489 */     TestUtil.runTest(TestAccessKeyGrant.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestAccessKeyGrant.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */